const GROUP_NAME = 'menus';
const {
  getMenus,
  addMenu,
  getMenuById,
  deleteMenuById,
  updateMenuById,
  isExist,
  menuSort,
} = require('../handler/menusHandler');

exports.plugin = {
  name: 'routerMenus',
  async register(server) {
    const { Joi, headers } = server;
    await server.route([

      { // 菜单排序
        method: 'put',
        path: `/${GROUP_NAME}/sort/{firstId}/{secondId}`,
        handler: menuSort,
        config: {
          notes: '菜单排序',
          description: '菜单排序（交换两个同级菜单的位置）',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
            params: Joi.object({
              firstId: Joi.string().required().hex().length(24)
                .description('第1个id')
                .error(() => new Error('第1个id非法')),
              secondId: Joi.string().required().hex().length(24)
                .description('第2个id')
                .error(() => new Error('第2个id非法')),
            }),
          },
        },
      },

      { // 获取菜单
        method: 'get',
        path: `/${GROUP_NAME}`,
        handler: getMenus,
        config: {
          notes: '获取菜单',
          description: '获取菜单',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
          },
        },
      },

      { // 添加菜单
        method: 'post',
        path: `/${GROUP_NAME}`,
        handler: addMenu,
        config: {
          notes: '添加菜单',
          description: '添加菜单',
          tags: ['api', GROUP_NAME],
          plugins: {
            'hapi-swagger': {
              payloadType: 'form',
            },
          },
          validate: {
            headers,
            payload: Joi.object({
              pid: Joi.string().hex()
                .length(24)
                .description('上级菜单id')
                .error(() => new Error('上级菜单id非法')),
              routename: Joi.string().required().description('路由名称')
                .error(() => new Error('路由名称必须填写')),
              routepath: Joi.string().required().description('路由路径')
                .error(() => new Error('路由路径必须填写')),
              component: Joi.string().required().description('组件路径')
                .error(() => new Error('组件路径必须填写')),
              status: Joi.boolean().required().default(true)
                .description('是否显示:false:隐藏,true:显示')
                .error(() => new Error('必须填写')),
              group: Joi.array().single()
                .description('所属组（权限）'),
              menuname: Joi.string().trim()
                .description('菜单名称'),
              menuicon: Joi.string().trim()
                .description('菜单图标'),
            }),
          },
        },
      },

      { // 根据id获取单个菜单信息
        method: 'GET',
        path: `/${GROUP_NAME}/{id}`,
        handler: getMenuById,
        config: {
          notes: '根据ID获取单个菜单信息',
          description: '根据ID获取单个菜单信息',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
            params: Joi.object({
              id: Joi.string().required().hex().length(24)
                .description('菜单id')
                .error(() => new Error('菜单id非法')),
            }),
          },
        },
      },


      { // 根据ID删除菜单
        method: 'delete',
        path: `/${GROUP_NAME}/{id}`,
        handler: deleteMenuById,
        config: {
          notes: '根据ID删除菜单',
          description: '根据ID删除菜单',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
            params: Joi.object({
              id: Joi.string().required().hex().length(24)
                .description('菜单id')
                .error(() => new Error('菜单id非法')),
            }),
          },
        },
      },

      { // 根据ID编辑菜单信息
        method: 'put',
        path: `/${GROUP_NAME}/{id}`,
        handler: updateMenuById,
        config: {
          notes: '根据ID更新菜单信息',
          description: '根据ID更新菜单信息',
          tags: ['api', GROUP_NAME],
          plugins: {
            'hapi-swagger': {
              payloadType: 'form',
            },
          },
          validate: {
            headers,
            params: Joi.object({
              id: Joi.string().required().hex().length(24)
                .description('菜单id')
                .error(() => new Error('菜单id非法')),
            }),
            payload: Joi.object({
              routename: Joi.string().required().description('路由名称')
                .error(() => new Error('路由名称必须填写')),
              routepath: Joi.string().required().description('路由路径')
                .error(() => new Error('路由路径必须填写')),
              component: Joi.string().required().description('组件路径')
                .error(() => new Error('组件路径必须填写')),
              status: Joi.boolean().required().default(true)
                .description('是否显示:false:隐藏,true:显示')
                .error(() => new Error('必须填写')),
              group: Joi.array().single()
                .description('所属组（权限）'),
              menuname: Joi.string().trim()
                .description('菜单名称'),
              menuicon: Joi.string().trim()
                .description('菜单图标'),
            }),
          },
        },
      },

      { // 判断字段值是否存在
        method: 'get',
        path: `/${GROUP_NAME}/exist`,
        handler: isExist,
        config: {
          notes: '判断字段值是否存在',
          description: '判断字段值是否存在',
          tags: ['api', GROUP_NAME],
          validate: {
            headers,
            query: Joi.object({
              field: Joi.string().required().allow('routename')
                .description('字段名')
                .error(() => new Error('字段名非法')),
              value: Joi.string().required()
                .description('值')
                .error(() => new Error('值必须填写')),
            }),
          },
        },
      },
    ]);
  },
};
